<template>
  <div>
    <div class="condition">
      <div style="float: left;margin-right:6px;padding-top:6px;font-size:12px;">所在科室:</div>
      <el-select v-model="deptCode" style="float: left;width:120px;" filterable placeholder="请选择科室" size="mini">
        <el-option v-for="item in deptList" v-bind:key="item.code" :label="item.name" :value="item.code"></el-option>
      </el-select>
      <div style="float: left;margin-left:10px;margin-right:6px;padding-top:6px;font-size:12px;">姓名或住院号:</div>
      <el-input style="float: left;width:160px;" placeholder="请输入姓名或住院号" v-model="ptName" size="mini">
      </el-input>
      <div style="float: left;margin-left:16px;padding-top:6px;"></div>
      <el-radio-group class="check-radio" v-model="status">
        <el-radio label="N">待审核</el-radio>
        <el-radio label="Y">已审核</el-radio>
      </el-radio-group>
      <div style="float: left;margin-left:10px;margin-right:6px;padding-top:6px;font-size:12px;">入院时间:</div>
      <el-date-picker
        v-model="value1"
        type="daterange"
        size= "mini"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
      <el-button type="primary" size= "mini" plain style="margin-left:10px;" @click="query()">查询</el-button>
    </div>
    <el-table
      size="mini" border
      :data="tableData"
      style="width: 100%;"
      :height="tableHeight">
      <el-table-column
        label="状态"
        width="60">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>姓名: {{ scope.row.name }}</p>
            <p>住址: {{ scope.row.address }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="mini" v-for="(item, index) in scope.row.tag" v-bind:key="index">{{ item.tagRemark }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="ptIoNo"
        label="住院号"
        width="110">
      </el-table-column>
      <el-table-column
        prop="ptName"
        label="姓名"
        width="100">
      </el-table-column>
      <el-table-column
        prop="drgs"
        label="预分组"
        width="90">
      </el-table-column>
      <el-table-column
        prop="series"
        label="住院次数"
        width="70">
      </el-table-column>
      <el-table-column
        prop="sex"
        label="性别"
        width="60">
      </el-table-column>
      <el-table-column
        prop="age"
        label="年龄"
        width="60">
      </el-table-column>
      <el-table-column
        prop="mainDiagName"
        label="主诊断"
        width="160">
      </el-table-column>
      <el-table-column
        prop="otherDiagName"
        label="其他诊断"
        width="160">
      </el-table-column>
      <el-table-column
        prop="mainOperCode"
        label="主要手术"
        width="160">
      </el-table-column>
      <el-table-column
        prop="otherOperCode"
        label="其他手术"
        width="160">
      </el-table-column>
      <el-table-column
        prop="days"
        label="住院天数"
        width="70">
      </el-table-column>
      <el-table-column
        prop="fee"
        label="总费用"
        width="100">
      </el-table-column>
      <el-table-column
        prop="ageChild"
        label="新生儿年龄(天)"
        width="110">
      </el-table-column>
      <el-table-column
        prop="weight"
        label="新生儿体重(g)"
        width="100">
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning"
            @click="handlePreDrgs(scope.$index, scope.row)">预分组</el-button>
          <el-button
            size="mini"
            type="primary"
            @click="handleCheck(scope.$index, scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style lang="scss">
.condition {
  padding-left: 16px;
  padding-top: 28px;
  text-align: left;
  height: 56px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
}

.check-radio{
  float: left;
  padding-top: 6px;

  .el-radio {
    margin-right: 6px;
  }
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>

<script>
import moment from 'moment'
import { Message } from 'element-ui'
import { getInDept } from '../../api/dept'
import { getDrgsCheckDtos, updatePageMedicalRecordStatus, preDrgs } from '../../api/page-medical-record'

export default {
  data () {
    return {
      tableHeight: window.innerHeight - 186,
      deptList: [],
      deptCode: '',
      value1: [moment().subtract(30, 'days'), new Date()],
      status: 'N',
      ptName: '',
      tableData: []
    }
  },
  methods: {
    // 查询
    query () {
      const param = {
        deptCode: this.deptCode,
        ptName: this.ptName,
        status: this.status,
        dateStart: moment(this.value1[0]).format('yyyy-MM-DD'),
        dateEnd: moment(this.value1[1]).format('yyyy-MM-DD' + ' 23:59:59')
      }
      const that = this
      getDrgsCheckDtos(param).then(res => {
        res.forEach(q => {
          q.tag = []
          // 没有drgs的情况下
          if (q.status === '' || q.status === 'N') {
            q.tag.push({
              tagRemark: '☹'
            })
          } else {
            q.tag.push({
              tagRemark: '☺'
            })
          }
          if (q.drgStatus === '2') {
            q.tag.push({
              tagRemark: '？'
            })
          } else if (q.drgStatus === '1') {
            q.tag.push({
              tagRemark: '✔'
            })
          } else if (q.drgStatus === '3') {
            q.tag.push({
              tagRemark: '✘'
            })
          }
        })
        that.tableData = res
      })
    },
    tableRowClassName ({ row, rowIndex }) {
      if (rowIndex === 1) {
        return 'warning-row'
      } else if (rowIndex === 3) {
        return 'success-row'
      }
      return ''
    },
    handlePreDrgs (index, row) {
      preDrgs({
        mainDiagnosisCode: row.mainDiagCode,
        nextDiagnosisCode: row.otherDiagCode,
        operationCode: row.mainOperCode + ((row.otherOperCode == null || row.otherOperCode === '') ? '' : (',', row.otherOperCode)),
        sex: row.sex === '男' ? 'F' : 'M',
        age: row.age,
        newbornAge: row.ageChild,
        newbornWeight: row.weight,
        daysStay: row.days,
        chargeStay: row.fee,
        leavingWay: '5'
      }).then(res => {
        row.drgs = res.drgs
      })
    },
    handleCheck (index, row) {
      updatePageMedicalRecordStatus({
        status: 'Y',
        visitId: row.visitId
      }).then(res => {
        if (res) {
          Message.success({
            message: '审核成功!'
          })
        }
      })
    }
  },
  mounted () {
    const that = this
    getInDept().then(res => {
      if (res.length > 0) {
        that.deptCode = res[0].code
      }
      that.deptList = res
    })
  }
}
</script>
